package com.hangwei.aicabinet.util;

import android.util.Log;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * author: 张龙臣
 * date: 2018-09-07.
 */

public class InstructionsUtil {

    /**
     * 从返回的指令中提取标签
     */
    public static HashMap<String, String> getTabFromResponse(String response) {
        HashMap<String, String> map = new HashMap<>();
        if (response.length() < 18)
            return null;
        //校验开头，状态码
        if (response.startsWith("FF") && "0000".equals(response.substring(6, 10))) {
            long length = ByteUtil.hexStr2decimal(response.substring(2, 4));
            //校验数据长度
            if (length * 2 + 14 != response.length()) { //推出来的
                Log.e("getTabFromResponse", "数据错误" + response);
                return null;
            }
            String allTab = response.substring(14, response.length() - 4);
            long tabNum = ByteUtil.hexStr2decimal(response.substring(12, 14));
            //校验标签数
            if (tabNum == 0) {
                return map;
            }
            int tabLength = (int) (allTab.length() / tabNum);
            String temp = allTab;

            for (int i = 0; i < tabNum; i++) {
                if (i == tabNum - 1) {
                    if (temp.length() < 8) {
                        Log.e("temp.length()", "数据错误" + temp);
                        return null;
                    }
                    map.put(temp.substring(8), "");
                } else {
                    String substring = temp.substring(0, tabLength);
                    if (substring.length() < 8) {
                        Log.e("substring(0,tabLength)", "数据错误" + temp.substring(0, tabLength));
                        return null;
                    }
                    map.put(substring.substring(8), "");
                    temp = temp.substring(tabLength);
                }
            }
            return map;
        }
        return null;
    }

    /**
     * 检验是否是返回标签的指令
     */
    public static boolean checkTabFromResponse(String response) {
        if (response.length() < 18)
            return false;
        if (!response.startsWith("FF") || !"0000".equals(response.substring(6, 10)))
            return false;
        long length = ByteUtil.hexStr2decimal(response.substring(2, 4));
        if (length * 2 + 14 != response.length())
            return false;
        long tabNum = ByteUtil.hexStr2decimal(response.substring(12, 14));
        return tabNum != 0;
    }

    /**
     * 从指令中提取可用天线
     */
    //FF 21 61 0000 05 0101 0200 0300 0400 0500 0600 0700 0801 0900 0A
    public static ArrayList<String> getAvailableAntenna(String back) {
        String str = back.substring(12, back.length() - 4);
        ArrayList<String> list = new ArrayList<>();
        String temp = str;
        while (true) {
            if (temp.length() == 4) {
                if ("01".equals(temp.substring(2)))
                    list.add(temp.substring(0, 2));
                break;
            } else {
                String s = temp.substring(0, 4);
                if ("01".equals(s.substring(2)))
                    list.add(s.substring(0, 2));
                temp = temp.substring(4);
            }
        }
        Log.d("找到可用天线", list.toString());
        return list;
    }

    /**
     * @param which 指定哪个柜门
     * @return 设置天线指令
     */
    public static String buildSetAntennaOrder(int which) {
        StringBuilder builder = new StringBuilder();
        switch (which) {
            case 1: //假设1号柜门是1天线
                buildA(builder, "0101");
                break;
            case 2:
                buildA(builder, "0202");
                break;
            case 3:
                buildA(builder, "0303");
                break;
            case 4:
                buildA(builder, "0404");
                break;
            case 5:
                buildA(builder, "0505");
                break;
            case 6:
                buildA(builder, "0606");
                break;
            case 7:
                buildA(builder, "0707");
                break;
            case 8:
                buildA(builder, "0808");
                break;
        }
        return builder.toString();
    }
    public static String buildSetAntennaOrderMove(int which) {
        StringBuilder builder = new StringBuilder();
        switch (which) {
            case 1: //假设1号柜门是1天线
                buildB(builder, "01010202");
                break;
            case 2:
                buildB(builder, "03030404");
                break;
            case 3:
                buildB(builder, "05050606");
                break;
            case 4:
                buildB(builder, "07070808");
                break;
            case 5:
                buildB(builder, "09090A0A");
                break;
            case 6:
                buildB(builder, "0B0B0C0C");
                break;
            case 7:
                buildB(builder, "0D0D0E0E");
                break;
            case 8:
                buildA(builder, "0F0F");
                break;
        }
        return builder.toString();
    }
//FF1F91020101020203030404050506060707080809090A0A0B0B0C0C0D0D0E0E0F0F4CB7

    private static void buildA(StringBuilder builder, String str) {
        builder.append("FF");       //添加数据头（固定）
        builder.append("03");       //计算并添加数据长度（天线个数*2+1）
        builder.append("91");       //操作码（固定）
        builder.append("02");       //Option（固定）
        builder.append(str); //根据实际情况手动配置
        String text = builder.toString().substring(2);
        byte[] bytes = ByteUtil.hexStr2bytes(text);
        byte[] crcTable = CRCUtil.crcTable(bytes, 0, text.length() / 2);
        String hexStr = ByteUtil.bytes2HexStr(crcTable);
        builder.append(hexStr);
    }
    private static void buildB(StringBuilder builder, String str) {
        builder.append("FF");       //添加数据头（固定）
        builder.append("05");       //计算并添加数据长度（天线个数*2+1）
        builder.append("91");       //操作码（固定）
        builder.append("02");       //Option（固定）
        builder.append(str); //根据实际情况手动配置
        String text = builder.toString().substring(2);
        byte[] bytes = ByteUtil.hexStr2bytes(text);
        byte[] crcTable = CRCUtil.crcTable(bytes, 0, text.length() / 2);
        String hexStr = ByteUtil.bytes2HexStr(crcTable);
        builder.append(hexStr);
    }

    public static String openLock(int which) {
        String str = "";
        switch (which) {
            case 1:
//                str = "01100014000810000100010001000100010001000100015D37";
                str = "0110001400010200016484";
                break;
            case 2:
                str = "0110001500010200016555";
                break;
            case 3:
                str = "0110001600010200016566";
                break;
            case 4:
                str = "01100017000102000164B7";
                break;
            case 5:
                str = "0110001800010200016448";
                break;
            case 6:
                str = "0110001900010200016599";
                break;
            case 7:
                str = "0110001A000102000165AA";
                break;
            case 8:
                str = "0110001B0001020001647B";
                break;
        }
        return str;
    }
//010300000008440C
//01100014000810000000000000000000000000000000002ADA
    public static String closeLock(int which) {
        String str = "";
        switch (which) {
            case 1:
                str = "011000140001020000 xxxx";
                break;
            case 2:
                break;
            case 3:
                break;
            case 4:
                break;
            case 5:
                break;
            case 6:
                break;
        }
        return str;
    }


}
// FF E2 2B 0000 00   0E标签个数
// 01 BF 01 0C E28011700000020A2B77C8BD
// 01C2010CE28011700000020A2B77C89D
// 01B7010CE28011700000020A2B77927D
// 01C3010CE28011700000020A2B77F42D
// 01BB010CE28011700000020A2B77F41D
// 01B6010CE28011700000020A2B77926D
// 01BA010CE28011700000020A2B77928D
// 01B8010CE28011700000020A2B77F4FD
// 01B8020CE28011700000020A2B784A5D
// 01C4020CE28011700000020A2B77F4ED
// 01BB020CE28011700000020A2B77C8CD
// 01BB020CE28011700000020A2B779CED
// 01BB020CE28011700000020A2B779CDD
// 01BD020CE28011700000020A2B77925D 9494

